package com.example.judge.rabbitmq;

import com.example.core.constants.RabbitMQConstants;
import com.example.api.domain.JudgeSubmitDTO;
import com.example.judge.service.judge.IJudgeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class JudgeConsumer {

    @Autowired
    private IJudgeService judgeService;

    @RabbitListener(queues = RabbitMQConstants.DIRECT_JUDGE_QUEUE_NAME)
    public void  processJudge(String message) {
        log.info("收到消息为: {}", message);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JudgeSubmitDTO dto = objectMapper.readValue(message, JudgeSubmitDTO.class);
            log.info("收到消息转换对象为: {}", dto);
            judgeService.doJudge(dto);
        } catch (JsonProcessingException e) {
            log.error("json转换错误", e);
        }
    }
}
